import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isFunction from "lodash/isFunction";
var _excluded = ["form", "editRecord", "modalProps", "onCancel", "isCreate"];

var _dec, _class;

/**
 * 个性化配置-列表 编辑表单
 * @date: 2018-9-29


 */
import React from 'react';
import { Input, Modal, Form } from 'antd';
import Switch from 'components/Switch';
import { MODAL_FORM_ITEM_LAYOUT } from 'utils/constants';
import { CODE_UPPER } from 'utils/regExp';
import intl from 'utils/intl';
var EditModal = (_dec = Form.create({
  fieldNameProp: null
}), _dec(_class = /*#__PURE__*/function (_React$Component) {
  _inherits(EditModal, _React$Component);

  var _super = _createSuper(EditModal);

  function EditModal(props) {
    var _this;

    _classCallCheck(this, EditModal);

    _this = _super.call(this, props);
    _this.state = {};
    _this.handleSaveBtnClick = _this.handleSaveBtnClick.bind(_assertThisInitialized(_this));
    _this.handleCleanForm = _this.handleCleanForm.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(EditModal, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          form = _this$props.form,
          editRecord = _this$props.editRecord,
          _this$props$modalProp = _this$props.modalProps,
          modalProps = _this$props$modalProp === void 0 ? {} : _this$props$modalProp,
          onCancel = _this$props.onCancel,
          _this$props$isCreate = _this$props.isCreate,
          isCreate = _this$props$isCreate === void 0 ? false : _this$props$isCreate,
          otherModalProps = _objectWithoutProperties(_this$props, _excluded);

      var initialValues = editRecord || {};
      return /*#__PURE__*/React.createElement(Modal, Object.assign({}, otherModalProps, modalProps, {
        onOk: this.handleSaveBtnClick,
        onCancel: onCancel,
        destroyOnClose: true,
        afterClose: this.handleCleanForm,
        width: 520
      }), /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, MODAL_FORM_ITEM_LAYOUT, {
        label: intl.get('cpfm.ui.model.page.pageCode').d('页面编码')
      }), form.getFieldDecorator('pageCode', {
        initialValue: initialValues.pageCode,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cpfm.ui.model.page.pageCode').d('页面编码')
          })
        }, {
          pattern: CODE_UPPER,
          message: intl.get('cpaas.common.validation.codeUpper').d('全大写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”')
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        disabled: !isCreate,
        trim: true,
        inputChinese: false,
        typeCase: "upper"
      }))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cpfm.ui.model.page.description').d('页面描述')
      }, MODAL_FORM_ITEM_LAYOUT), form.getFieldDecorator('description', {
        initialValue: initialValues.description
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(Form.Item, Object.assign({
        label: intl.get('cpaas.common.status.enable').d('启用')
      }, MODAL_FORM_ITEM_LAYOUT), form.getFieldDecorator('enabledFlag', {
        initialValue: initialValues.enabledFlag !== 0 ? 1 : 0
      })( /*#__PURE__*/React.createElement(Switch, null)))));
    }
  }, {
    key: "handleSaveBtnClick",
    value: function handleSaveBtnClick() {
      var _this$props2 = this.props,
          _this$props2$isCreate = _this$props2.isCreate,
          isCreate = _this$props2$isCreate === void 0 ? false : _this$props2$isCreate,
          editRecord = _this$props2.editRecord,
          onOk = _this$props2.onOk,
          form = _this$props2.form;

      if (_isFunction(onOk)) {
        form.validateFields(function (err, data) {
          if (!err) {
            if (isCreate) {
              onOk(data);
            } else {
              onOk(_objectSpread(_objectSpread({}, editRecord), data));
            }
          }
        });
      }
    }
  }, {
    key: "handleCleanForm",
    value: function handleCleanForm() {
      var form = this.props.form;
      form.resetFields();
    }
  }]);

  return EditModal;
}(React.Component)) || _class);
export { EditModal as default };